/**
 * Created by Wing on 2017/5/25.
 */


var showeditNoteTypeView = function () {
	 
    // 1.拿到用户的所有笔记类型
    $.getJSON(
        "/memorypalace/notetype/getmynotetype.action",
        function (noteTypeList) {
        	//noteType=noteTypeList;
        	//console.log(noteType);
            $("#noteTypeList>ul").html("");
            $.each(noteTypeList,function (i, noteType) {
            	
                var h = '<li class="list-group-item" id="'+noteType.noteTypeId+'">'+noteType.noteTypeName+'</li>';
    // 2.添加到列表中
                $("#noteTypeList>ul").append(h);
            });
         
    // 3.所有行可以点击，点完了有一个selected类加上去
            $("#noteTypeList>ul>li").click(function (event) {
                var li = $(event).attr("delegateTarget");
                $(li).toggleClass("active");
            });
            $("#noteTypeList>ul>li:first-child").trigger("click");
        }
    );
    
};

var showeditNoteTypePage = function () {
	var noteTypeId;
	// 1.拿到有active class的li
	// 如果有多个，报错
		//alert("类型大于一");
	var active=$("#noteTypeList .active");
	if(active.length==1){
		noteTypeId=$(active[0]).attr("id");
	}else{
	    showTip("一次只能编辑一个笔记类型",undefined,false);
	    return;
    }
	var deckId;

	var Request=undefined; 
	Request=new getRequest(); 
	deckId=Request["deckId"];
	//alert($(".active").attr("id"));
	// 如果有一个，拿到id
	
	//console.log(noteTypeId);
    window.location.href="/memorypalace/notetype.html?deckId="+deckId+"&noteTypeId="+noteTypeId;
    // location.replace("/memorypalace/notetype.html?deckId="+deckId+"&noteTypeId="+noteTypeId);
}

var showeditNoteArea = function () {
    var noteTypeId;
    // 1.拿到有active class的li
    // 如果有多个，报错
    //alert("类型大于一");
    var active=$("#noteTypeList .active");
    if(active.length==1){
        noteTypeId=$(active[0]).attr("id");
    }else{
        showTip("一次只能编辑一个笔记类型",undefined,false);
        return;
    }
    var deckId;

    var Request=undefined;
    Request=new getRequest();
    deckId=Request["deckId"];
    $("#areaNamesInput").val("");
    $.getJSON(
        "/memorypalace/notetype/getnotetypebyid.action?noteTypeId=" + noteTypeId,
        function (noteType) {
            var areaNames = JSON.parse(noteType.areaNames);
            var str = "";
            $.each(areaNames,function (i, areaName) {
                if(i == 0){
                    str += areaName;
                }else{
                    str += ","+areaName;
                }
            })
            $("#areaNamesInput").val(str);
            $("#noteTypeNameInput").val(noteType.noteTypeName);
        }
    );
}

var areaNamesInputOnChange = function (input) {

}

var editNoteTypeArea = function (ev) {
    var str = $("#areaNamesInput").val();
    var reg = /[^\u4e00-\u9fa5,]+/;
    var o = reg.test(str);
    // 判断是否满足条件
    if(false){
    // 如果不满足,则提示
        $("#areaErrorTip").html("不允许有除了英文逗号以外的符号");
    }else{
    // 如果满足则发送请求,成功后"点击"x,还有名称也传过去
        // 修改当前笔记类型的区域
        // 对笔记内容进行修改
        // 只用修改笔记类型和笔记的区域名就好.
        $("#editNoteAreaClose span").click();
    }

}
var createNewNoteType = function () {
    var str = $("#newNoteTypeInput").val();
    // 发送请求
    $("#createNewNoteTypeClose span").click();
    showeditNoteTypeView();
}
var deleteNoteType = function () {
    var noteTypeId;
    // 1.拿到有active class的li
    // 如果有多个，报错
    //alert("类型大于一");
    var active=$("#noteTypeList .active");
    if(active.length==1){
        noteTypeId=$(active[0]).attr("id");

        $.dialog("confirm","删除","您将删除一个笔记类型!",0,function () {
            showTip("删除成功",undefined,true);
        });
    }else{
        $.dialog("confirm","删除","您将删除多个笔记类型!",0,function () {
            showTip("删除成功",undefined,true);
        });
        return;
    }
}